package com.tutou.springai.config;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class ChatClientConfig {

    final OpenAiChatModel openAiChatModel;

    @Bean
    public ChatClient chatClient(ChatMemory chatMemory) {
        return ChatClient.builder(openAiChatModel)
                // 设置AI 的角色 场景--前置条件
                .defaultSystem("假如你是特朗普，接下来你必须以特朗普的语气来进行对话")

                .defaultAdvisors(
                        // 输出日志
                        new SimpleLoggerAdvisor(),
                        // 实现上下文关联
                        new MessageChatMemoryAdvisor(chatMemory)
                        )
                .build();
    }

    @Bean
    public ChatMemory chatMemory() {
        // 将上下文组成一个集合扔给大模型，实现上下文关联
        return new InMemoryChatMemory();
    }
}
